/**
 * Definition for a binary tree node.
 * function TreeNode(val, left, right) {
 *     this.val = (val===undefined ? 0 : val)
 *     this.left = (left===undefined ? null : left)
 *     this.right = (right===undefined ? null : right)
 * }
 */
/**
 * @param {number[]} preorder
 * @param {number[]} inorder
 * @return {TreeNode}
 */
 var buildTree = function(preorder, inorder) {
    let map=new Map();
    inorder.map((value,index)=>{
        map.set(value,index);
    })
   preorder_n=0;  // 递归会先处理左边再处理右边，所以和前序遍历的顺序的一样的，前序遍历只需按顺序走即可，将中序遍历一直切割成叶子节点
   function dfs(inorder_start,inorder_end){
       if(inorder_start>inorder_end){
           preorder_n--;
           return null;
       }
       if(inorder_start==inorder_end){
           return new TreeNode(preorder[preorder_n],null,null);
       }
       let inorder_n=map.get(preorder[preorder_n]);
       let newnode=new TreeNode(preorder[preorder_n]);
       preorder_n++;
       newnode.left=dfs(inorder_start,inorder_n-1);
       preorder_n++;
       newnode.right=dfs(inorder_n+1,inorder_end);
       return newnode;
   }
   return dfs(0,inorder.length-1)
};